package pl.edu.agh.gronoMining.owlToGraphConverter.parser.implementation;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.ParseException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import pl.edu.agh.gronoMining.owlToGraphConverter.graph.Graph;
import pl.edu.agh.gronoMining.owlToGraphConverter.parser.ILineParser;
import pl.edu.agh.gronoMining.owlToGraphConverter.parser.utils.IConverter;
import pl.edu.agh.gronoMining.owlToGraphConverter.parser.utils.IDataManager;
import pl.edu.agh.gronoMining.owlToGraphConverter.parser.utils.NodeData;

public class LineParser1 implements ILineParser {

	private IDataManager dataManager;
	private IConverter converter;
	
	public LineParser1(IDataManager dataManager, IConverter converter) {
		this.dataManager = dataManager;
		this.converter = converter;
	}
	
	@Override
	public void prepare() {
		
	}
	
	@Override
	public Graph[] parse(InputStream in) throws IOException, ParseException {
		BufferedReader reader = new BufferedReader(new InputStreamReader(in));
		String line;
		Set<Graph> graphsSet = new HashSet<Graph>();
		
		Graph currentGraph = null;
		Map<Integer, Integer> currentGraphVertexMap = new HashMap<Integer, Integer>();
		
		int lineNo = 1;
		while ((line = reader.readLine()) != null) {
			String[] lineElements = line.trim().split(" ");
			
			if ("t".equals(lineElements[0])) {
				
				if (currentGraph != null)
					graphsSet.add(currentGraph);
				currentGraph = new Graph();
				currentGraphVertexMap = new HashMap<Integer, Integer>();
				
			} else if ("v".equals(lineElements[0])) {
				
				int number = Integer.parseInt(lineElements[1]);
				int label = Integer.parseInt(lineElements[2]);
				NodeData data = dataManager.getData(label);
			
				int type = data.getNodeType();
				Object value = data.getNodeValue();
				String text = "" + type;
				if (value != null) {
					text += " - ";
					if (type == NodeData.SZUKA) {
						text += converter.getLookForText((Integer) value);
					} else if (type == NodeData.DATA_REJESTRACJI) {
						text += converter.getRegistryDateText((Integer) value);
					} else if (type == NodeData.WIEK) {
						text += converter.getAgeText((Integer) value);
					} else if (type == NodeData.CZAS_LOGOWANIA) {
						text += converter.getLastLogTimeText((Integer) value);
					} else if (type == NodeData.PLEC) {
						text += converter.getSexText((Integer) value);
					} else if (type == NodeData.WIELKOSC_MIASTA) {
						text += converter.getCitySizeText((Integer) value);
					} else if (type == NodeData.ILOSC_LUDZI_WOJEWODZTWA) {
						text += converter.getProvinceSizeText((Integer) value);
					} else if (type == NodeData.ZAMOZNOSC_WOJEWODZTWA) {
						text += converter.getProvinceWealthText((Integer) value);
					} else if (type == NodeData.DATA_ZALOZENIA_GRONA) {
						text += converter.getGronoStartDateText((Integer) value);
					} else if (type == NodeData.WIELKOSC_GRONA) {
						text += converter.getGronoSizeText((Integer) value);
					} else {
						text += data.getNodeValue();
					}
				}
				
				int id = currentGraph.addNode(type,label, text);
				currentGraphVertexMap.put(number, id);
				
			} else if ("e".equals(lineElements[0])) {
				
				int id1 = Integer.parseInt(lineElements[1]);
				int id2 = Integer.parseInt(lineElements[2]);
				currentGraph.addEdge(currentGraphVertexMap.get(id1), currentGraphVertexMap.get(id2));
				
			} else if ("=>".equals(lineElements[0])) {
				
			} else {
				throw new ParseException("unknown identifier",lineNo);
			}
			lineNo++;
		}
		
		if (currentGraph != null)
			graphsSet.add(currentGraph);
		
		Graph[] graphArray = new Graph[graphsSet.size()];
		int i = 0;
		for (Graph g : graphsSet)
			graphArray[i++] = g;
		return graphArray;
	}
}
